import { _Cls_Points } from "../body/att/_Points";
import { _Cls_Att } from "../body/att/_Att";
import buff_item from "./buff_item";
import { BuffEffect } from "../../../shared/interface";

export default class buffs {
    list: buff_item[] = [];
    keys: Map<BuffEffect, number> = new Map();
    P: _Cls_Points = new _Cls_Points();
    att: _Cls_Att = new _Cls_Att();
    constructor() {

    }
    clearAll() {
        this.list = [];
        this.keys = new Map();
    }
    getBuff(key: BuffEffect) {
        for (let i = 0; i < this.list.length; i++) {
            const e = this.list[i];
            if (e.key == key) {
                return e;
            }
        }
    }
    addBuff(bf: buff_item) {
        this.checkTime();
        let update = true;
        // 通过list item.from_id  检测唯一性  如果有重复的则调用item的cover 方法
        const existingIndex = this.list.findIndex(item => `${item.key}_${item.from_id}` === `${bf.key}_${bf.from_id}`);
        if (existingIndex != -1) {
            console.log('覆盖buff', bf)
            // 如果找到，则调用现有buff的cover方法进行处理
            update = this.list[existingIndex].cover(bf);
            this.setVal(bf.key, bf.val);
        } else {
            console.log('新增buff', bf)
            this.list.push(bf);
            this.addVal(bf.key, bf.val);
        }
        if (update) {
            this.updateatt();
        }
    }
    delBuff(buff_id: string) {
        let expiredBuffs: number[] = []; // 用于存储将要删除的buff的索引
        // 检查每个buff是否过期
        this.list.forEach((buff, index: number) => {
            if (buff.from_id != buff_id) {
                return;
            }
            console.log('buff删除', index, '过期')
            expiredBuffs.push(index); // 记录过期buff的索引
            let currentVal = this.keys.get(buff.key) || 0; // 获取当前key的值
            currentVal -= buff.val; // 减去过期buff的值
            this.keys.set(buff.key, currentVal); // 更新key的值
        });

        // 从后向前删除过期的buff，避免影响索引
        for (let i = expiredBuffs.length - 1; i >= 0; i--) {
            this.list.splice(expiredBuffs[i], 1);
        }
        if(expiredBuffs.length > 0){
            this.updateatt();
        }
    }
    private updateatt() {
        this.P.clearAll();
        this.att.clearAll();
        this.att.reset({
            Hp_Max: this.getVal(BuffEffect.最大生命),
            Hp_res: this.getVal(BuffEffect.生命恢复速度),
            Mp_Max: this.getVal(BuffEffect.最大魔法值),
            Mp_res: this.getVal(BuffEffect.魔法恢复速度),
            Physical_Attack_max:this.getVal(BuffEffect.最大物攻),
            Physical_Attack_min:this.getVal(BuffEffect.最小物攻),
            Physical_Ls: this.getVal(BuffEffect.物理吸血),
            Physical_crit:this.getVal(BuffEffect.物理暴击率),
            Physical_defense: this.getVal(BuffEffect.物理防御),
            Magic_Attack_max: this.getVal(BuffEffect.最大魔攻),
            Magic_Ls: this.getVal(BuffEffect.魔法吸血),
            Magic_defense: this.getVal(BuffEffect.魔法防御),
            Magic_crit: this.getVal(BuffEffect.魔法暴击率),
            Magic_Attack_min: this.getVal(BuffEffect.最小魔攻),
            Attack_Speed: this.getVal(BuffEffect.攻击速度),
        })
    }
    private checkTime() {
        const currentTime = Date.now(); // 获取当前时间戳
        let expiredBuffs: number[] = []; // 用于存储将要删除的buff的索引

        // 检查每个buff是否过期
        this.list.forEach((buff, index: number) => {
            if (buff.endTime == -1) {
                return;
            }
            if (currentTime >= buff.endTime) { // 如果buff已过期
                console.log('buff', index, '过期')
                expiredBuffs.push(index); // 记录过期buff的索引
                let currentVal = this.keys.get(buff.key) || 0; // 获取当前key的值
                currentVal -= buff.val; // 减去过期buff的值
                this.keys.set(buff.key, currentVal); // 更新key的值
            }
        });

        // 从后向前删除过期的buff，避免影响索引
        for (let i = expiredBuffs.length - 1; i >= 0; i--) {
            this.list.splice(expiredBuffs[i], 1);
        }
        if(expiredBuffs.length > 0){
            this.updateatt();
        }
    }
    addVal(k: BuffEffect, val: number) {
        let _val = this.getVal(k);
        _val += val;
        this.keys.set(k, _val);
    }
    setVal(k: BuffEffect, val: number) {
        this.keys.set(k, val);
    }
    getVal(k: BuffEffect) {
        this.checkTime();
        return this.keys.get(k) || 0;
    }
}